package com.wzx.ptplatform.controller.admin.set;

import com.wzx.ptplatform.domain.Politics;
import com.wzx.ptplatform.service.PoliticsService;
import com.wzx.ptplatform.utils.LayuiPager;
import com.wzx.ptplatform.utils.LayuiUtils;
import com.wzx.ptplatform.utils.MapUtils;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author 紫煦桉
 */
@Api(tags = "管理员其他模块——政治面貌管理")
@RestController
public class AdminSetPoliticsController {

    final PoliticsService politicsService;

    public AdminSetPoliticsController(PoliticsService politicsService) {
        this.politicsService = politicsService;
    }

    @RequestMapping("admin_set_politics_message_list")
    public LayuiPager<Politics> adminSetPoliticsMessageList(
            String politicsType,
            @RequestParam(defaultValue = "1", value = "page") Integer page,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit) {
        Politics politics = new Politics();
        if (politicsType != null && !"".equals(politicsType)) {
            politics.setPoliticsType(politicsType);
        }
        Page<Politics> pager = politicsService.findList(politics, page - 1, limit);
        LayuiPager<Politics> layuiPager = LayuiUtils.initPager(page, limit);
        LayuiUtils.setPager(layuiPager, pager.getContent(), Math.toIntExact(pager.getTotalElements()));
        return layuiPager;
    }

    @GetMapping("admin_set_politics_message_addView")
    public ModelAndView adminSetPoliticsMessageAddView() {
        return new ModelAndView("admin/set/politics_message_add");
    }

    @GetMapping("admin_set_politics_message_editView")
    public ModelAndView adminSetPoliticsMessageEditView(String id, Model model) {
        model.addAttribute("politics", politicsService.findById(Long.parseLong(id)));
        return new ModelAndView("admin/set/politics_message_edit");
    }

    @PostMapping("admin_set_politics_message_save_or_edit")
    public Map<String, Object> adminSetPoliticsMessageSaveOrEdit(Politics politics) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (politicsService.saveOrEdit(politics)) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_set_politics_message_delete")
    public Map<String, Object> adminSetPoliticsMessageDelete(String id) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (politicsService.deleteById(Long.parseLong(id))) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_set_politics_message_delete_select")
    public Map<String, Object> adminSetPoliticsMessageDeleteSelect(String arrayId) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (arrayId != null && !"".equals(arrayId)) {
            String[] array = arrayId.split(",");
            for (String id : array) {
                if (id != null && !"".equals(id)) {
                    politicsService.deleteById(Long.parseLong(id));
                }
            }
            MapUtils.successMap(map);
        }
        return map;
    }
}
